package com.rongsheng.mpchartdemo;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import cn.qqtheme.framework.picker.DatePicker;
import cn.qqtheme.framework.util.ConvertUtils;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
    private ListView listView;
  private Button bt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.listview);
        bt = (Button) findViewById(R.id.bt_time);
        listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, initListData()));
        listView.setOnItemClickListener(this);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                      startTime(v);
            }
        });

    }

    private List<String> initListData() {
        List<String> data = new ArrayList<>();
        data.add("柱状图（单）");
        data.add("饼图");
        data.add("水平柱状图");
        data.add("波浪");
        data.add("时间选择");
        return data;
    }

    @Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent i;
        switch (position) {
            case 0:
                i = new Intent(MainActivity.this, BarChartActivity.class);
                startActivity(i);
                break;
            case 1:
                i = new Intent(MainActivity.this, PieChartActivity.class);
                startActivity(i);
                break;
            case 2:
                i = new Intent(MainActivity.this,HorizontalBarChartActivity.class);
                startActivity(i);
                break;
            case 3:
                i = new Intent(MainActivity.this,WaveActivity.class);
                startActivity(i);
                break;

        }
    }

    private void startTime(View view) {
        Calendar now = Calendar.getInstance();
        int year=now.get(Calendar.YEAR);
        int mouth=now.get(Calendar.MONTH)+1;
        int day=now.get(Calendar.DAY_OF_MONTH);
        final DatePicker picker = new DatePicker(this);
        picker.setGravity(Gravity.CENTER);
        picker.setCanceledOnTouchOutside(true);
        picker.setUseWeight(true);
        picker.setTopPadding(ConvertUtils.toPx(this, 10));
        picker.setRangeEnd(2111, 1, 11);
        picker.setRangeStart(2014, 1, 1);
        picker.setSelectedItem(year, mouth, day);
        picker.setResetWhileWheel(false);
        picker.setOnDatePickListener(new DatePicker.OnYearMonthDayPickListener() {
            @Override
            public void onDatePicked(String year, String month, String day) {
                showToast(year + "-" + month + "-" + day);
                bt.setText(year + "-" + month + "-" + day);
            }
        });
        picker.setOnWheelListener(new DatePicker.OnWheelListener() {
            @Override
            public void onYearWheeled(int index, String year) {
                picker.setTitleText(year + "-" + picker.getSelectedMonth() + "-" + picker.getSelectedDay());
            }

            @Override
            public void onMonthWheeled(int index, String month) {
                picker.setTitleText(picker.getSelectedYear() + "-" + month + "-" + picker.getSelectedDay());
            }

            @Override
            public void onDayWheeled(int index, String day) {
                picker.setTitleText(picker.getSelectedYear() + "-" + picker.getSelectedMonth() + "-" + day);
            }
        });
        picker.show();


    }
    private void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}